package com.baida.retail.tasksystem.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by eric on 2020/4/23.
 */
public class StringUtils {

    public static String generateRandomString(int length) {
        char[] elements = {'3', '4', '5', '7', '8', 'a', 'c', 'd', 'e', 'f',
                'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u',
                'v', 'w', 'x', 'y'};
        StringBuilder stringBuilder = new StringBuilder();

        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < length; i++) {
            stringBuilder.append(elements[random.nextInt(elements.length)]);
        }
        return stringBuilder.toString();
    }

    public static String buildConnectedParamsWithKeySorted(Map<String, String> params) {
        StringBuilder content = new StringBuilder();
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (value != null) {
                content.append(i == 0 ? "" : "&").append(key).append("=").append(value);
            } else {
                content.append(i == 0 ? "" : "&").append(key).append("=");
            }
        }
        return content.toString();
    }

    //产生6位随机数字
    public static String randomSmsCode() {
        int max = 1000000;
        Random random = new Random(System.currentTimeMillis());
        int value = random.nextInt(max);
        return String.format("%06d", value);
    }
}
